//
//  YTTRunroadController.m
//  跑圈儿
//
//  Created by 唐康 on 16/4/19.
//  Copyright © 2016年 唐康. All rights reserved.
//

#import "YTTRunroadController.h"
#import "step_yuan.h"
#import <CoreMotion/CoreMotion.h>
#import <Masonry.h>

#define num 0.005
@interface YTTRunroadController ()
@property(nonatomic,strong)CMMotionManager *MotionManager;
/**< str
 */
@property(nonatomic, assign)CGFloat stepper;
/**< <#注释#>  */
@property(nonatomic, strong)UILabel *stepLab;
/**< step  */
@property(nonatomic, assign)NSInteger stepCount;
/**< 注释  */
@property(nonatomic, strong)UIImageView *bg;
@end

@implementation YTTRunroadController




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"跑步有道";
     [self setupBg];
    [self countStep];
   
    
 
    

    
}
/**
 *  背景
 */
- (void)setupBg{
    UIImageView *bg = [[UIImageView alloc]initWithFrame:YTTBOUNDS];
    bg.image = [UIImage imageNamed:@"bg_healthkit"];
    [self.view addSubview:bg];
    self.bg = bg;
}
/**
 *  计步处理
 */
- (void)countStep{
    _step = [[step_yuan alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
    self.step.center = self.view.center;
    _step.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_step];

    
    
    UILabel *stepLab = [[UILabel alloc]initWithFrame:CGRectMake(50, 90, 150, 60)];
    [self.step addSubview:stepLab];
    //stepLab.backgroundColor = [UIColor redColor];
    
    stepLab.textColor = YTTRGBColor(255, 255, 255);
    stepLab.text = @"0";
    stepLab.textAlignment = NSTextAlignmentCenter;
    stepLab.font = [UIFont systemFontOfSize:50];
    self.stepLab = stepLab;
    
    
    
    
    self.stepper = 0;
    self.stepCount = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    self.MotionManager = [[CMMotionManager alloc]init];
    
    if (_MotionManager.accelerometerAvailable) {
        _MotionManager.accelerometerUpdateInterval = 0.1;
        [_MotionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData,NSError *error){
            if (error) {
                [_MotionManager stopAccelerometerUpdates];
                NSLog(@"error");
            }else{
                
                
                if (fabs(accelerometerData.acceleration.x)>2.0 || fabs(accelerometerData.acceleration.y)>2.0 || fabs(accelerometerData.acceleration.z)>2.0 ) {
                    if (self.stepCount < 10000) {
                        self.stepLab.text = [NSString stringWithFormat:@"%zd",self.stepCount++];
                    }
                    self.stepper++;
                    
                    
                    _step.z =(self.stepper  ) / 10000;
                }
                
                
            }
        }];
    }else{
        NSLog(@"This device has no accelerometer");
    }
}





@end
